using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using BaseScripts;
using XEntity.InventoryItemSystem;
public class Player : Status
{

        static public Player instance = null;

        private Animator animator;

        protected override void Awake() {
            base.Awake();
            Player.instance = this;
        }

        protected override void Start()
        {
            base.Start();
            animator = GetComponent<Animator>();
            foreach (var item in ItemManager.Instance.itemList){
                GetComponent<Interactor>().inventory.AddItem(item);
            }
        
        }


        protected override void Update()
        {
            base.Update();

            Vector2 dir = Vector2.zero;
            if (Input.GetKey(KeyCode.A))
            {
                dir.x = -1;
                animator.SetInteger("Direction", 3);
            }
            else if (Input.GetKey(KeyCode.D))
            {
                dir.x = 1;
                animator.SetInteger("Direction", 2);
            }

            if (Input.GetKey(KeyCode.W))
            {
                dir.y = 1;
                animator.SetInteger("Direction", 1);
            }
            else if (Input.GetKey(KeyCode.S))
            {
                dir.y = -1;
                animator.SetInteger("Direction", 0);
            }

            dir.Normalize();
            animator.SetBool("IsMoving", dir.magnitude > 0);
            
            if (bCanMove)
                if (dir.magnitude < 0.5f && rb.velocity.magnitude > 0.5f){//添加一个运动方向反向的力
                    GetComponent<Rigidbody2D>().AddForce(- base.accelSpeed * rb.velocity.normalized * Time.deltaTime);
                }

            GetComponent<Rigidbody2D>().AddForce(base.accelSpeed * dir * Time.deltaTime);
            if (rb.velocity.magnitude > base.speed){
                rb.velocity = rb.velocity.normalized * base.speed;
            }
        }

    #region "重写继承函数"
    public override void GetHit()
    {
        base.GetHit();
        animator.SetTrigger("Hit");
    }
    protected override void Dead()
    {
        base.Dead();
        animator.SetBool("Dead", true);
    }
    #endregion
}
